* Figure 2: Trends in Intergenerational Mobility by Birth Year

clear
use "${data_dir}DavisMazumderData.dta"

* Sample Restrictions 
keep if age_firstSurvey<=18
drop if miss0 | miss1
drop if (mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) 
 

 
* Generate income ranks after applying sample restrictions
gen N = .
 tab n  if surv79==0 & women==0  [w=weight]
 replace N = r(N) if surv79==0 & women==0 
 tab n  if surv79==0 & women==1   [w=weight]
 replace N = r(N) if surv79==0 & women==1 
 tab n  if surv79==1 & women==0   [w=weight]
 replace N = r(N) if surv79==1 & women==0 
 tab n  if surv79==1 & women==1 [w=weight]
 replace N = r(N) if surv79==1 & women==1 
 
* Family Income Rank in Parent Generation
set sortseed 6688571
sort women surv79 faminc0
by women surv79: gen __rank0 = sum(weight) 
gen _rank0 = __rank0/N 
by women surv79 faminc0: egen rank0 = mean(_rank0) 
 replace rank0 = 100*rank0
gen perc66 = rank0*(1-surv79)
gen perc79 = rank0*surv79

 
* Family Income Rank in Kid Generation
set sortseed 6688571
sort women surv79 faminc1
by women surv79: gen __rank1 = sum(weight)
gen _rank1 = __rank1/N 
bys women surv79 faminc1: egen rank1 = mean(_rank1) 
replace rank1 = 100*rank1

drop N __rank0 _rank0 __rank1 _rank1 
 
 

* Generate interactions with year of birth
levelsof byear, local(levels) 
di "`levels'"

foreach y in `levels' {
 gen p`y' = rank0*(byear==`y')
}


gen byear_66 = byear*(survey==1966)
gen byear_79 = byear*(survey==1979)
qui su byear if survey==1966
gen byear_66_rank0 = (byear_66-r(mean))*rank0 if survey==1966
 replace byear_66_rank0 = 0 if survey==1979
qui su byear if survey==1966
gen byear_79_rank0 = (byear_79-r(mean))*rank0 if survey==1979
 replace byear_79_rank0 = 0 if survey==1966
gen rank0_66 = rank0*(survey==1966)
gen rank0_79 = rank0*(survey==1979)



reg rank1 p1948-p1964 i.byear women [w=weight], cluster(hhid)
matrix rank= J(10,3,.)
local i =0
foreach y in `levels' {
 local i=`i'+1
 matrix rank[`i',1] = `y'
 matrix rank[`i',2] = _b[p`y']
 matrix rank[`i',3] = _se[p`y']
}

foreach y in `levels' {
 gen f0_`y' = lfaminc0*(byear==`y')
}

qui su byear if survey==1966
gen byear_66_lfaminc0 = (byear_66-r(mean))*lfaminc0 if survey==1966
 replace byear_66_lfaminc0 = 0 if survey==1979
qui su byear if survey==1966
gen byear_79_lfaminc0 = (byear_79-r(mean))*lfaminc0 if survey==1979
 replace byear_79_lfaminc0 = 0 if survey==1966
gen lfaminc0_66 = lfaminc0*(survey==1966)
gen lfaminc0_79 = lfaminc0*(survey==1979)


reg lfaminc1 lfaminc0 lfaminc0_79 byear_66_lfaminc0 byear_79_lfaminc0 surv79 i.byear women [w=weight], cluster(hhid) 

* Robust to excluding 1961
reg lfaminc1 loginc66 loginc79 surv79 women [w=weight] if byear!=1961, cluster(hhid)
lincom loginc79-loginc66

reg lfaminc1 f0_1948-f0_1964 i.byear women [w=weight], cluster(hhid)
matrix ige= J(10,3,.)
local i =0
foreach y in `levels' {
 local i=`i'+1
 matrix ige[`i',1] = `y'
 matrix ige[`i',2] = _b[f0_`y']
 matrix ige[`i',3] = _se[f0_`y']
}


clear
svmat rank
gen l95 = rank2-1.96*rank3
gen u95 = rank2+1.96*rank3

graph twoway (scatter rank2 rank1) (rcap u95 l95 rank1, lcolor(black)) (lpoly rank2 rank1, degree(3) lcolor(gs10) lpattern(dash)), graphregion(color(white)) legend(off) xtitle("Birth Year") ytitle("Rank-Rank Slope") ylabel(0(0.2)0.8,nogrid) title("Rank-Rank")
graph save ${results_dir}figure2_rank.gph, replace


clear 
svmat ige
gen l95 = ige2-1.96*ige3
gen u95 = ige2+1.96*ige3

graph twoway (scatter ige2 ige1) (rcap u95 l95 ige1, lcolor(black)) (lpoly ige2 ige1, degree(3) lcolor(gs10) lpattern(dash)), graphregion(color(white)) legend(off) xtitle("Birth Year") ytitle("IGE") ylabel(0(0.2)0.8,nogrid) title("IGE")
graph save ${results_dir}figure2_ige.gph, replace





graph combine ${results_dir}figure2_rank.gph ${results_dir}figure2_ige.gph, graphregion(color(white)) ycommon
graph export ${results_dir}figure2.png, replace


exit
